iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 21
0
自我挑戰組

練習程式系列 第 21

Android,CameraX、第三方camera套件 、 Touch Pinch

  • 分享至 

  • xImage
  •  

教學來源:
照著步驟完成:Getting Started with CameraX

Google Jetpack 新组件 CameraX 介绍与实践

CameraX overview

整理:

1

camerax最少要Android 5.0 (API level 21)。所以這邊至少要21:
https://ithelp.ithome.com.tw/upload/images/20190930/20111994QyqzLIGVgs.png

2

camerax主要三個功能:
Preview(圖像預覽): 看到的畫面設定(還沒要拍照存檔前的畫面)
Image analysis(圖像分析): 取得圖片的數據
Image capture(拍照): 拍下的照片設定,像是閃光

3

模擬器只有1G跑照相功能會很慢,所以改成2G:
Tool-- >AVD Manager-- >筆-- > show advance setting
https://ithelp.ithome.com.tw/upload/images/20190930/20111994eMBJXPWkQL.png

4

想來設定閃光模式,閃光是Image capture(拍照功能)。所以是imageCaptureConfig的設定:
https://developer.android.com/reference/androidx/camera/core/ImageCaptureConfig.Builder
https://ithelp.ithome.com.tw/upload/images/20190930/20111994OvvF2g4vMD.png

點選FlashMode:
可以看到有三個參數:AUTO(自動閃光)、OFF、ON
https://ithelp.ithome.com.tw/upload/images/20190930/20111994aXJi2u896B.png

所以程式:

        val imageCaptureConfig = ImageCaptureConfig.Builder()
                .apply {
                    setFlashMode(FlashMode.AUTO) //閃光自動
                }.build()

不過用模擬器,目前不知道有沒有閃光,之後要再測試。(真正的手機有閃光)

5

想用個按鈕,開關閃光:
參考:Camera flash is not working in android
目前想法:
https://ithelp.ithome.com.tw/upload/images/20190930/20111994BBh204iDfU.png
程式(失敗):

private var flashmode: Boolean = false

        btnFlash.setOnClickListener{
            if( flashmode == false){
                flashmode= true
                ImageCaptureConfig.Builder()
                        .apply {
                            setFlashMode(FlashMode.ON)
                        }
                btnFlash.setText("閃光啟動中")

            }else{
                flashmode= false
                ImageCaptureConfig.Builder()
                        .apply {
                            setFlashMode(FlashMode.OFF)
                        }
                btnFlash.setText("閃光沒開")

            }
        }

目前想到是直接給個boolean值,如果true就FlashMode.ON,如果false就FlashMode.OFF,不過這樣就要在刷新一次相機。所以要先關閉camerax,在啟動時調整閃光參數。
關於關閉camerax:
What is the correct way of starting and stopping camera using CameraX?
使用:

CameraX.unbindAll()

整理解答:

一 為麼要viewFinder.post { startCamera() }?而不直接startCamera()?
在回去複習https://codelabs.developers.google.com/codelabs/camerax-getting-started/#4

Instead of calling startCamera() on the main thread, we use viewFinder.post { ... } to make sure that viewFinder has already been inflated into the view when startCamera() is called.

6

找到一個camerax的教學:
Introdução a CameraX API

7

平常設計畫面都只有直的,橫的就會變的很奇怪,實際要調整也很簡單
參考:
How to Create Separate Layout Files for Landscape Mode and Different Screen Sizes - Android Tutorial
https://ithelp.ithome.com.tw/upload/images/20191110/20111994ptZbNwhNxs.png

8

Imagebutton點選換圖案,像是閃光有自動、開、關,就可以用三個圖
參考:
Android Imagebutton change Image OnClick
顯示圖案方式:

btn.setImageResource(R.drawable.flash);

9

想要用兩隻手指放大縮小相機畫面,目前失敗:
目前測試放大縮小只能各一個,而不是慢慢放大,或慢慢縮小
參考:How to zoom the preview using Android CameraX library?
參考解答的程式:https://github.com/Akshayrraiyani/CameraX_Zoom_In_Out

先來了解OnGestureListener、OnTouchListener,教學:
Android Gestures: Getting Started

之後參考這些,完成放大縮小:

Android Gestures: Getting Started
GestureDetector中onFling()与onScroll()的区别
Android: Detecting a Pinch Gesture
ViewConfiguration
onTouchListener warning: onTouch should call View#performClick when a click is detected
Listeners with several functions in Kotlin. How to make them shine?
jerrellmardis/ZoomFragment.java
RedApparat/Fotoapparat

關於Fotoapparat 問題

Can't change picture resolution
畫素與解析度是什麼?
How to change pictureResolution in Fotoapparat Camera?
Android Camera Library Comparison

1

就算有firstAvailable()這個,還是會錯誤:

                pictureResolution = firstAvailable(
                    { Resolution(720, 1280) },
                    highestResolution()
                )

錯誤的時候會顯示,有哪些Resolution:

Resolution configuration selector selected value Resolution(width=720, height=1280). However it's not in the supported set of values. Supported parameters: [Resolution(width=4128, height=3096), Resolution(width=4128, height=2322), Resolution(width=3264, height=2448), Resolution(width=3264, height=1836), Resolution(width=2560, height=1920), Resolution(width=2048, height=1536), Resolution(width=2048, height=1152), Resolution(width=1152, height=1152), Resolution(width=1920, height=1080), Resolution(width=1280, height=960), Resolution(width=1280, height=720), Resolution(width=640, height=480), Resolution(width=320, height=240)]

或是這個錯誤:

    io.fotoapparat.exception.camera.InvalidConfigurationException: Resolution configuration selector selected value Resolution(width=1280, height=720). However it's not in the supported set of values. Supported parameters: [Resolution(width=640, height=480), Resolution(width=352, height=288), Resolution(width=320, height=240)]

2

D/Fotoapparat: Orientations: 
    Screen orientation (preview) is: Orientation.Vertical.Portrait. 
    Camera sensor orientation is always at: Orientation.Horizontal.Landscape. 

所以如果Portrait(直)的拍照,要自己選轉90度,參考:
Controlling the camera to take pictures in portrait doesn't rotate the final images
how to save bitmap to android gallery
Fotoapparat: Why does landscape CameraView captures in portrait mode?

3 要參考這個方法,找最接近 Resolution(1920, 1080) 的:

How to select middle resolution? #377
不過update還是失敗:

                    fotoapparat?.updateConfiguration(
                        UpdateConfiguration(
                            pictureResolution = {
                                nearestBy(
                                    Resolution(1920, 1080),
                                    Resolution::area
                                )
                            }
                        )
                    )

不太知道為什麼。是因為修改的是pictureResolution嗎?閃光update就沒問題 。

4

focusmode 有這些:

[FocusMode.Auto, FocusMode.Infinity, FocusMode.Fixed, FocusMode.Macro, FocusMode.ContinuousFocusVideo, FocusMode.ContinuousFocusPicture]

CameraKit

CameraKit Documentation - v1.0.0-beta3.11
Build出含有版本號的SDK

其他:
這樣就可以看 語法的資訊了:
How to view method information in Android Studio?
選取變數 , 然後 crtl +Q


上一篇
Android, GridView , ViewPager, Image Slider ,API29問題
下一篇
Android、java,RecyclerView
系列文
練習程式37
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言